package com.diuse.outer.analyse.tongcheng;

import com.diuse.outer.common.Constant;
import org.apache.log4j.Logger;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/11/21 0021.
 */
public class SummaryHtmlParser {
    private List<Map<String, String>> summaryMapList = new ArrayList<Map<String, String>>();
    private String url;
    private static final String UrlKey = "url";
    private static final String PriKey = "price";
    private static final String PublishTimeKey = "publishTime";
    private static final String RoomKey = "roomKey";
    private static Logger logger = Logger.getLogger(SummaryHtmlParser.class);
    private static int count = 0;
    private static int MAX_PAGE = 2;
    private static String ENCODE = "GBK";

    public SummaryHtmlParser(String url) {
        this.url = url;
    }

    public List<Map<String, String>> analyse() {
        for (int i = 1; i <= MAX_PAGE; i++) {
            String tempUrl = url.concat("/pn").concat(String.valueOf(i));
            System.out.println(tempUrl);
            doAnalyse(tempUrl);
        }
        return summaryMapList;
    }

    public void doAnalyse(String urlTemp) {
        try {
            Parser parser = new Parser((new URL(urlTemp)).openConnection());
            NodeFilter filterTable = new TagNameFilter("table");
            NodeFilter classFilter = new HasAttributeFilter("class", "tbimg");
            NodeFilter filter = new AndFilter(filterTable, classFilter);
            NodeList nodes = parser.extractAllNodesThatMatch(filter);
            if (nodes != null) {
                for (int i = 0; i < nodes.size(); i++) {
                    Node node = (Node) nodes.elementAt(i);
                    String html = node.toHtml();
                    int index = html.indexOf("</table>");
                    html = html.substring(0, index);
                    intialSumarryMap(html);
                }
            }
        } catch (Exception e) {
            logger.error(Constant.Log.ANALYSE_SUMARRY_ERROR, e.getCause());
//            e.printStackTrace();
        }

    }

    private static void message(String szMsg) {
        try {
            System.out.println(new String(szMsg.getBytes(ENCODE), System.getProperty("file.encoding")));
        } catch (Exception e) {

        }
    }


    public static void main(String[] args) {
        new SummaryHtmlParser("http://hz.58.com/gudang/zufang").analyse();

    }

    private void testTrimInfo() {
        String info = "<tr logr=\"q_2_29227970274052_24083585241145_4_2\">\n" +
                "    <td class=\"img\">\n" +
                "        <div class=\"img_list\">\n" +
                "                                            <span class=\"img_picCount\">\n" +
                "                            <span class=\"img_picCountOpacity\"></span>\n" +
                "                            <span>8 图</span>\n" +
                "                        </span>\n" +
                "            <a href=\"http://jump.zhineng.58.com/clk?target=mv7V0A-b5HThmvqfpv--5yOdUAkhuA-1pyEqnHEvn1nLPWcznH9OrjcOnjc3rjn1nHnQnjcdFMP_ULEqUA-1IAQ-uMEhuA-1IAYqnHEYrjc3njNdPjELPBuzuymqpZwY0jCfsv6lsWN3shPfUiqMIyw6Uh0fXMRhmyOMszu1ULRzmvNqniuvpgPdpyEqnauvpgPdpgTqnWD3sWDkri3zPHD8nWTYFhR8IZwk5HTh0A7zmyYqFh-1ug6YuyOb5yu6UZP-FMK_uWYVniuGUyRG5iudpyEqnWbznW0OP1TzP1EkPHchmvQopyEqPhuhuHNkryDVPHcdmBYYnjI-syc3rHDVnHnYPh7-rHRhnhNvFMKGujYOFhR8IA-b5HcYnj91PH9dnWEQnHEdFhR8IZwk5HTh0ZFGmvNqnHcknauzuywG0WdCIZwkrBtfpZC8PH98mvqVsLGduh78uztzPjT3n1N3PHcYnHDYPg980v6YUykhuA-10Aq15HEh0LRBpyEqnH9zPWNLn1NhmLF-mgw-ujYQPWEkPW0Ln1Edn1D1nW9h0A7zmydGujYhuA-1mvDqrau1uyQhmvDqraubpgP_U1Y3PHbdFMP-UAu_U1Y3PHbdFMDqniukmyI-UMRV5HDhmh-b5HDznjThIv-_uAP60hEqnau60ZK-Uhwoug-LULFb5HTh0hRWmyQ_uhQfI1YkFh7BIjdw&version=&psid=146337622189829028833131025&entinfo=24083585241145_0\"\n" +
                "               target=\"_blank\">\n" +
                "                <img src='http://t2.58cdn.com.cn/bidding/small/n_v1bl2lwkf5erivmoyqqnhq.jpg' alt=\"翠苑物华小康\"/>\n" +
                "            </a>\n" +
                "        </div>\n" +
                "    </td>\n" +
                "    <td class=\"t qj-rentd\">\n" +
                "\n" +
                "        <a href=\"http://jump.zhineng.58.com/clk?target=mv7V0A-b5HThmvqfpv--5yOdUAkhuA-1pyEqnHEvn1nLPWcznH9OrjcOnjc3rjn1nHnQnjcdFMP_ULEqUA-1IAQ-uMEhuA-1IAYqnHEYrjc3njNdPjELPBuzuymqpZwY0jCfsv6lsWN3shPfUiqMIyw6Uh0fXMRhmyOMszu1ULRzmvNqniuvpgPdpyEqnauvpgPdpgTqnWD3sWDkri3zPHD8nWTYFhR8IZwk5HTh0A7zmyYqFh-1ug6YuyOb5yu6UZP-FMK_uWYVniuGUyRG5iudpyEqnWbznW0OP1TzP1EkPHchmvQopyEqPhuhuHNkryDVPHcdmBYYnjI-syc3rHDVnHnYPh7-rHRhnhNvFMKGujYOFhR8IA-b5HcYnj91PH9dnWEQnHEdFhR8IZwk5HTh0ZFGmvNqnHcknauzuywG0WdCIZwkrBtfpZC8PH98mvqVsLGduh78uztzPjT3n1N3PHcYnHDYPg980v6YUykhuA-10Aq15HEh0LRBpyEqnH9zPWNLn1NhmLF-mgw-ujYQPWEkPW0Ln1Edn1D1nW9h0A7zmydGujYhuA-1mvDqrau1uyQhmvDqraubpgP_U1Y3PHbdFMP-UAu_U1Y3PHbdFMDqniukmyI-UMRV5HDhmh-b5HDznjThIv-_uAP60hEqnau60ZK-Uhwoug-LULFb5HTh0hRWmyQ_uhQfI1YkFh7BIjdw&version=&psid=146337622189829028833131025&entinfo=24083585241145_0\"\n" +
                "           target=\"_blank\" class=\"t\" onclick=\"\" rel=\"nofollow\">翠苑物华小康居住2室2厅82平米豪华装修押一付三</a>\n" +
                "        <!----------------网邻通年限增加位置----------------------------->\n" +
                "        <span class=\"wlt-ico wlt1\" title=\"58同城认证商家\"></span>\n" +
                "\n" +
                "        <span class=\"jingpin\"></span>\n" +
                "        <span name=\"zaixian_29227970274052\"\n" +
                "              onClick=\"clickLog('from=fc_pc_list_bangbang&psid=146337622189829028833131025&entityId=24083585241145&entityType=0')\"></span>\n" +
                "        <i class=\"clear\"></i>\n" +
                "        </p><p class=\"qj-renaddr\">\n" +
                "        <!------------------------------小区相关------------------------------>\n" +
                "        <a href=\"/cuiyuan/zufang/\" class=\"a_xq1\"> 翠苑</a>\n" +
                "        - <a href=\"http://hz.58.com/xiaoqu/wuhuaxiaokangjuzhuqu/chuzu/\" target=\"_blank\" class=\"a_xq1\">物华小康居住区租房</a>\n" +
                "\n" +
                "        <span>/</span>\n" +
                "        今天\n" +
                "\n" +
                "    </p>\n" +
                "\n" +
                "        <p class=\"qj-rendp\">\n" +
                "\t\t           \t<span class=\"qj-listjjr\">\n" +
                "       \t\t\t       \t\t\t\t<span class=\"geren\"></span>\n" +
                "       \t        \t\t\t                   \t<label>经纪人：</label>\n" +
                "               \t<a class=\"qj-lijjrname\" target=\"_blank\"\n" +
                "                   onclick=\"javascript:clickLog('from=fc_list_pxjjr&psid=146337622189829028833131025&entityId=24083585241145&entityType=0');\">曹兴乾</a>\n" +
                "\t\t\t                   \t                   \t<a target=\"_blank\"\n" +
                "                                                       onclick=\"javascript:clickLog('from=fc_list_pxgs&psid=146337622189829028833131025&entityId=24083585241145&entityType=0');\">我爱我家</a>\n" +
                "       \t\t\t       \t\t\t                   \t<label>-</label>\n" +
                "                   \t<a target=\"_blank\"\n" +
                "                       onclick=\"javascript:clickLog('from=fc_list_pxmd&psid=146337622189829028833131025&entityId=24083585241145&entityType=0');\">西斗门租赁</a>\n" +
                "               \t               </span>\n" +
                "        </p>\n" +
                "    </td>\n" +
                "    <td><b class='pri'>3400</b> 元/月\n" +
                "        <br/>\n" +
                "        <span class=\"showroom\">2室2厅1卫</span>\n" +
                "        <br/>\n" +
                "    </td>";
        trimInfo(info);
        System.exit(0);
    }


    private void intialSumarryMap(String table) {
        final String TR_START = "<tr";
        final String TR_END = "</tr>";
        int start;
        int end;

        start = table.indexOf(TR_START);
        while (start > -1) {
            end = table.indexOf(TR_END, start);
            String tr = table.substring(start, end);
            trimInfo(tr);
            count++;
            start = table.indexOf(TR_START, end);
        }
    }

    private static void trimInfo(String info) {
        String tdStart = "<td";
        String tdEnd = "</td>";
        Map<String, String> listMap = new HashMap<String, String>();

        List<String> tdList = new ArrayList<String>();
        int start = info.indexOf(tdStart);
        int end;
        while (start > -1) {
            end = info.indexOf(tdEnd, start);
            if (end == -1) {
                logger.error(Constant.Log.HTHL_FORMAT_ERROR + info);
                break;
            }
            tdList.add(info.substring(start, end));
            start = info.indexOf(tdStart, end);
        }
        String second = tdList.get(1);
        int urlStart = second.indexOf("<a href=\"");
        int urlEnd = second.indexOf("\"", urlStart + 1 + "<a href=\"".length());
        String url = second.substring(urlStart + "<a href=\"".length(), urlEnd);
        System.out.println("detail link: " + url);
        listMap.put(UrlKey, url);

        String publishStart;
        int pStart = second.indexOf("<p class=\"qj-renaddr\">");
        pStart = second.indexOf("</span>", pStart);
        int pEnd = second.indexOf("</p>", pStart + "</span>".length());
        int pEnd2 = second.indexOf("<span>", pStart + "</span>".length());
        String publish = second.substring(pStart + "</span>".length(), pEnd);
        System.out.println("publish time: " + publish.trim());
        listMap.put(PublishTimeKey, publish);

        String third = tdList.get(2);
        int priceStart = third.indexOf("<b class='pri'>");
        int priceEnd = third.indexOf("</b>", priceStart + "<b class='pri'>".length());
        String price = third.substring(priceStart + "<b class='pri'>".length(), priceEnd);
        System.out.println("price : " + price + "元/月");
        listMap.put(PriKey, price);

        int roomStart = third.indexOf("<span class=\"showroom\">");
        int roomEnd = third.indexOf("</span>", roomStart + "<span class=\"showroom\">".length());
        String room = third.substring(roomStart + "<span class=\"showroom\">".length(), roomEnd);
        System.out.println("room : " + room);
        listMap.put(RoomKey, room);
        try {
            new DetailHtmlParser(url).analyse();
        } catch (IOException e) {
            logger.error("IO Exception analyse url: " + url);

            e.printStackTrace();
        } catch (ParserException e) {
            logger.error("Parser Exception analyse url: " + url);
            e.printStackTrace();
        }
        System.out.println("==========================================================================");

    }


}
